package com.example.myapplication.messenger;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.myapplication.R;

public class MessengerDemoActivity extends AppCompatActivity {
    Button msgBtn;
    boolean mbind = false;
    Messenger rMessenger = null;///来自服务器的信使
    Messenger mMessageer = null;///来自客户端的信使

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MessageService.MSG_SET_VALUE:
                    ///在这里进行服务器发来消息的处理
                    ///修改按钮上的文字
                    msgBtn.setText(msg.getData().getString("data2"));
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    };


    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            Log.e("message", "---------------------连接成功");
            rMessenger = new Messenger(iBinder);
            mbind = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("message", "---------------------连接断开");
            mbind = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messenger_demo);
        msgBtn = findViewById(R.id.msg_btn);
        mMessageer = new Messenger(handler);

    }

    public void seyHello(View view) {

        if(!mbind) return;
        Message msg = Message.obtain(null, MessageService.MSG_SAY_HELLO);
        Bundle bundle = new Bundle();
        bundle.putString("data","今天过得好吗");
        msg.setData(bundle);

        msg.replyTo = mMessageer;
        Log.e("TAG", "replyTo: --0"+msg.replyTo);

        ///客户端向服务端发送消息
        try {
            rMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("message", "---------------------onStart");
        Intent intent = new Intent(this,MessageService.class);
        Log.e("message", "---------------------onStart2");
        bindService(intent,mConnection, Context.BIND_AUTO_CREATE);
        Log.e("message", "---------------------onStart3");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("message", "---------------------onStop");
        if(mbind){
            unbindService(mConnection);
            mbind = false;
        }
    }
}